# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Renato Silva <br.renatosilva@gmail.com>

_realname=drmingw
pkgbase="mingw-w64-${_realname}"
pkgname=(${MINGW_PACKAGE_PREFIX}-${_realname})
pkgver=0.9.2
pkgrel=2
pkgdesc="Just-in-Time (JIT) debugger (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
license=(LGPL2.1)
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
makedepends=(${MINGW_PACKAGE_PREFIX}-discount
             ${MINGW_PACKAGE_PREFIX}-cmake
             ${MINGW_PACKAGE_PREFIX}-gcc
             ${MINGW_PACKAGE_PREFIX}-python3)
url='https://github.com/jrfonseca/drmingw'
source=(${_realname}-${pkgver}.tar.gz::"https://github.com/jrfonseca/drmingw/archive/${pkgver}.tar.gz"
        import-libs.patch
        install-files.patch
        gcc-10.2.0.patch)
options=(!strip staticlibs)
sha256sums=('a09536e1fd7f0908baa68f7414d57e81fc4b02fe5a63fdd1db73d12f00d775c1'
            '2098bb85c6fdd4c9879d1c23702b570087443bcd56f083563126f3ac93e14a93'
            '25f67789665a02ed47396bf3af85447a045ae0bb1d23991d66571243aae6bbfb'
            'c49e6f69437aed84884dfa756d3e5fa35031ac63d95e3a817db71a775be57a4b')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  #patch -p1 -i ${srcdir}/import-libs.patch
  patch -p1 -i "${srcdir}/install-files.patch"
  patch -p1 -i "${srcdir}/gcc-10.2.0.patch"
}

build() {
  [[ -d ${srcdir}/build-${MINGW_CHOST} ]] && rm -rf ${srcdir}/build-${MINGW_CHOST}
  mkdir -p ${srcdir}/build-${MINGW_CHOST} && cd ${srcdir}/build-${MINGW_CHOST}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DPYTHON_EXECUTABLE=${MINGW_PREFIX}/bin/python3.exe \
    ../${_realname}-${pkgver}

  make
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  make DESTDIR=${pkgdir} -j1 install
  
  mv ${pkgdir}${MINGW_PREFIX}/bin/addr2line{,-drmingw}.exe

  # License files
  cd "${pkgdir}${MINGW_PREFIX}/share"
  mkdir -p licenses/${_realname}
  mv doc/${_realname}/LICENSE* licenses/${_realname}

  # Readme
  cd "${pkgdir}${MINGW_PREFIX}/share/doc/${_realname}"
  markdown README.md > README.html
  rm README.md
}
